<template>
  <div class="app-container">
    <!--导入弹出框-->
    <export-file
      :is-show="editExportVisible"
      :add-path="'/resource/importAppend'"
      :cover-path="'/resource/importReplace'"
      @changeShow="updateList($event)"
    />

    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
    >
      <el-form-item :label="$t('staff.search')" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          :placeholder="$t('operating.pleaseWrite')"
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item :label="$t('history.deptname')" prop="departmentId">
        <treeselect
          v-model="queryParams.departmentId"
          :placeholder="$t('operating.pleaseChosen')"
          :show-count="true"
          :max-height="600"
          :options="depData"
          :default-expand-level="1"
          style="width: 150px; margin-right: 10px"
          @input="nodeClick"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >{{ $t("operating.search") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("operating.reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >{{ $t('common.caddkey') }}</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBitchDelete"
        >{{ $t('common.cdelete') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >{{ $t("operating.export") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleExport"
        >{{ $t("operating.import") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:export']"
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handledownloadExp"
        >{{ $t("staff.downloadExp") }}</el-button>
      </el-col>
    <!-- <right-toolbar
        :show-search.sync="showSearch"
        @queryTable="getList"
      /> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      class="staffTable"
      style="width: 100%"
      :height="tableHeight"
      @selection-change="handleSelectionChange"
    >
      <!--多选框-->
      <el-table-column
        type="selection"
        min-width="50"
        align="center"
      />
      <el-table-column
        min-width="100"
        :label="$t('staff.departmentName')"
        prop="departmentName"
      />
      <el-table-column
        min-width="100"
        :label="$t('staff.people')"
        prop="name"
      />
      <el-table-column
        min-width="100"
        :label="$t('staff.sex')"
        prop="sex"
      />
      <el-table-column
        min-width="100"
        :label="$t('staff.resourceId')"
        prop="resourceId"
      />
      <el-table-column
        min-width="100"
        :label="$t('staff.tagId')"
        prop="tagId"
      />
      <el-table-column
        min-width="120"
        :label="$t('staff.workTypeName')"
        prop="workTypeName"
      />
      <el-table-column
        min-width="120"
        :label="$t('staff.phoneNum')"
        prop="phoneNum"
      />
      <el-table-column
        min-width="150"
        :label="$t('staff.email')"
        prop="email"
      />
      <el-table-column
        :label="$t('realtime.rateHeart')"
        prop="rateHeart"
      />

      <el-table-column
        min-width="100"
        :label="$t('staff.onlineState')"
        prop="onlineState"
      >
        <template slot-scope="scope">
          <el-button
            icon="el-icon-check"
            style="font-size:12px"
            :type="scope.row.onlineState === 1 ? 'success' : 'info'"
            circle
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('operating.operating')"
        min-width="165"
        fixed="right"
      >
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail()"
          >{{ $t("operating.seeDetails") }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
          >{{ $t("operating.handle") }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >{{ $t("operating.delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item
              :label="$t('staff.departmentName')"
              prop="depIdx"
              :rules="filter_rules({ required: '请选择部门' })"
            >
              <el-select
                v-model="form.depIdx"
                filterable
                :placeholder="$t('operating.pleaseChosen')"
              >
                <el-option
                  v-for="item in deptSelect"
                  :key="item.idx"
                  :label="item.name"
                  :value="item.idx"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--人员名称-->
            <el-form-item
              :label="$t('staff.people')"
              prop="name"
              :rules="filter_rules({ required: '请输入人员名称' })"
            >
              <el-input
                v-model="form.name"
                :placeholder="$t('operating.pleaseWrite')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--人员性别-->
            <el-form-item
              :label="$t('staff.sex')"
              prop="sex"
              :rules="filter_rules({ required: '请选择人员性别' })"
            >
              <el-select
                v-model="form.sex"
                filterable
                :placeholder="$t('operating.pleaseChosen')"
              >
                <el-option
                  v-for="item in sexOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--人员编号-->
            <el-form-item
              :label="$t('staff.resourceId')"
              prop="idx"
              :rules="filter_rules({ required: '请输入人员编号' })"
            >
              <el-input
                v-model="form.idx"
                :placeholder="$t('operating.pleaseWrite')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--绑定标签-->
            <el-form-item :label="$t('staff.tagId')" prop="tagId">
              <el-input
                v-model="form.tagId"
                :placeholder="$t('operating.pleaseWrite')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--工种类型-->
            <el-form-item :label="$t('staff.workTypeName')">
              <el-select
                v-model="form.workTypeId"
                filterable
                :placeholder="$t('operating.pleaseChosen')"
              >
                <el-option
                  v-for="item in workTypeList"
                  :key="item.idx"
                  :label="item.name"
                  :value="item.idx"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--负责人电话-->
            <el-form-item :label="$t('staff.phoneNum')" prop="phoneNum">
              <el-input
                v-model="form.phoneNum"
                :placeholder="$t('operating.pleaseWrite')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--负责人邮箱-->
            <el-form-item
              :label="$t('staff.email')"
              prop="email"
              :rules="filter_rules({ type: 'email' })"
            >
              <el-input
                v-model="form.email"
                :placeholder="$t('operating.pleaseWrite')"
              />
            </el-form-item>
            <!--职位-->
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('staff.nation')" prop="nation">
              <el-input
                v-model="form.nation"
                :placeholder="$t('operating.pleaseWrite')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--技能-->
            <el-form-item :label="$t('staff.skill')" prop="skill">
              <el-input
                v-model="form.skill"
                :placeholder="$t('operating.pleaseWrite')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--身份证号-->
            <el-form-item :label="$t('staff.idNum')" prop="idNum">
              <el-input
                v-model="form.idNum"
                :placeholder="$t('operating.pleaseWrite')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!--备注-->
            <el-form-item :label="$t('staff.remark')" prop="remark">
              <el-input
                v-model="form.remark"
                :placeholder="$t('operating.pleaseWrite')"
                type="textarea"
                maxlength="100"
                :rows="2"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog
      title="查看"
      :visible.sync="details"
      width="900px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item
              :label="$t('staff.departmentName')"
              prop="depIdx"
              :rules="filter_rules({ required: '请选择部门' })"
            >
              <span>{{ form.depIdx }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <!--人员名称-->
            <el-form-item
              :label="$t('staff.people')"
              prop="name"
              :rules="filter_rules({ required: '请输入人员名称' })"
            >
              <span>{{ form.name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--人员性别-->
            <el-form-item
              :label="$t('staff.sex')"
              prop="sex"
              :rules="filter_rules({ required: '请选择人员性别' })"
            >
              <span>{{ form.sex }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--人员编号-->
            <el-form-item
              :label="$t('staff.resourceId')"
              prop="idx"
              :rules="filter_rules({ required: '请输入人员编号' })"
            >
              <span>{{ form.resourceId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--绑定标签-->
            <el-form-item :label="$t('staff.tagId')" prop="tagId">
              <span>{{ form.tagId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--工种类型-->
            <el-form-item :label="$t('staff.workTypeName')">
              <span>{{ form.workTypeId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--负责人电话-->
            <el-form-item :label="$t('staff.phoneNum')" prop="phoneNum">
              <span>{{ form.phoneNum }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--负责人邮箱-->
            <el-form-item
              :label="$t('staff.email')"
              prop="email"
              :rules="filter_rules({ type: 'email' })"
            >
              <span>{{ form.email }}</span>
            </el-form-item>
            <!--职位-->
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('staff.nation')" prop="nation">
              <span>{{ form.nation }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--技能-->
            <el-form-item :label="$t('staff.skill')" prop="skill">
              <span>{{ form.skill }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--身份证号-->
            <el-form-item :label="$t('staff.idNum')" prop="idNum">
              <span>{{ form.idNum }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!--备注-->
            <el-form-item :label="$t('staff.remark')" prop="remark">
              <el-input
                v-model="form.remark"
                :placeholder="$t('operating.pleaseWrite')"
                type="textarea"
                maxlength="100"
                :rows="2"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="details = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import exportFile from '@/components/exportFile/exportFile'

import {
  findPageStaff,
  department,
  resourcedelete,
  departmentlist,
  workTypeAll,
  resourcesave
} from '@/api/system/staff'

export default {
  name: 'Post',
  components: { Treeselect, 'export-file': exportFile },
  data() {
    return {
      editExportVisible: false,
      sexOptions: [
        {
          value: '男',
          label: '男'
        },
        {
          value: '女',
          label: '女'
        }
      ],
      deptName: null,
      deptValue: null,
      depData: [],
      details: false,
      deptSelect: null,
      Idx: null,
      workTypeList: [],
      tableHeight: window.innerHeight - 400,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        departmentId: undefined

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: '岗位名称不能为空', trigger: 'blur' }
        ],
        postCode: [
          { required: true, message: '岗位编码不能为空', trigger: 'blur' }
        ],
        postSort: [
          { required: true, message: '岗位顺序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
    this.deparlist()
  },
  methods: {

    /** 查询岗位列表 */
    getList() {
      this.loading = true
      findPageStaff(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => {
        return {
          'idx': item.idx
        }
      })
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /**
     * 批量删除
     */
    handleBitchDelete() {
      this.deleteData(this.$i18n.t('common.cdelTip'), this.ids)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = []
      postIds.push({ idx: row.idx })
      this.deleteData(this.$i18n.t('common.cdelTipItem') + row.name, postIds)
    },

    deleteData(delTip, delData) {
      this.$confirm(delTip, this.$i18n.t('common.cwarn'), {
        confirmButtonText: this.$i18n.t('common.cok'),
        cancelButtonText: this.$i18n.t('common.ccancel'),
        type: 'warning'
      }).then(function() {
        return resourcedelete(delData)
      }).then((response) => {
        this.getList()
        this.msgSuccess(response.msg)
      })
    },

    updateList(e) {
      this.editExportVisible = e
      this.getList()
      this.deparlist()
    },
    handleNodeClick(val) {
      console.log(val)
      this.queryParams.departmentId = val.id
      this.getList()
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    //  获取部门列表
    getDept() {
      departmentlist().then((response) => {
        this.deptSelect = response.data
      })
    },
    //  获取工种列表
    getWorkTypeList() {
      workTypeAll().then((response) => {
        this.deptSelect = response.data
        const obj = { idx: 0, name: '不设置' }
        this.deptSelect.unshift(obj)
        this.workTypeList = this.deptSelect
      })
    },
    deparlist() {
      department().then((response) => {
        this.depData = response.data
      })
    },
    nodeClick(val) {
      const self = this
      // 可将地图的ID设置为树结构的ID，通过点击节点，来切换地图
      this.queryParams.departmentId = val
      // this.getStaffList();
    },
    // // 导出所有
    // reportAll() {
    //   if (this.queryParams.departmentId) {
    //     window.location.href =
    //       baseUrl +
    //       '/resource/reportAll?departmentId=' +
    //       this.queryParams.departmentId +
    //       '&token=' +
    //       Cookie.get('token')
    //   } else {
    //     window.location.href =
    //       baseUrl + '/resource/reportAll?token=' + Cookie.get('token')
    //   }
    // },
    // 下载模版
    handledownloadExp() {
      window.location.href =
        'http://192.168.3.114:8080/' +
        '/resource/getTemplates?token=' +
        'f184752feb29d3fbaa37bebd1a1731ec'
    },
    // 查看
    handleDetail() {
      this.details = true
    },

    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        depIdx: undefined,
        idx: undefined,
        tagId: undefined,
        name: undefined,
        sex: undefined,
        phoneNum: undefined,
        email: undefined,
        nation: undefined,
        skill: undefined,
        idNum: undefined,
        remark: undefined,
        workTypeId: undefined
      }
      this.resetForm('form')
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加职工'
      this.getDept()
      this.getWorkTypeList()
    },
    /** 修改按钮操作 11*/
    handleUpdate(row) {
      this.reset()
      const postId = row.postId || this.ids
      getPost(postId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改岗位'
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.Idx = row.idx
      this.open = true
      this.title = '编辑职工'
      this.getDept()
      this.getWorkTypeList()
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.selectIndex = index

      this.form = {
        depIdx: row.departmentId,
        idx: row.idx,
        tagId: row.tagId,
        name: row.name,
        sex: row.sex,
        phoneNum: row.phoneNum,
        email: row.email,
        nation: row.nation,
        skill: row.skill,
        idNum: row.idNum,
        remark: row.remark,
        workTypeId: row.workTypeId
      }
      // 设置编辑框为可见状态
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title != '编辑职工') {
            const param = {
              resourceId: this.form.idx,
              name: this.form.name,
              sex: this.form.sex,
              phoneNum: this.form.phoneNum,
              email: this.form.email,
              nation: this.form.nation,
              skill: this.form.skill,
              idNum: this.form.idNum,
              remark: this.form.remark,
              tagId: this.form.tagId,
              remark: this.form.remark,
              departmentId: this.form.depIdx,
              type: 'staff',
              workTypeId:
                this.form.workTypeId === undefined || this.form.workTypeId === 0
                  ? 0
                  : this.form.workTypeId
            }

            resourcesave(param).then((response) => {
              if (response.code == '200') {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            const param = {
              resourceId: this.form.idx,
              name: this.form.name,
              sex: this.form.sex,
              phoneNum: this.form.phoneNum,
              email: this.form.email,
              nation: this.form.nation,
              skill: this.form.skill,
              idNum: this.form.idNum,
              remark: this.form.remark,
              tagId: this.form.tagId,
              remark: this.form.remark,
              departmentId: this.form.depIdx,
              type: 'staff',
              idx: this.Idx,
              workTypeId:
                this.form.workTypeId === undefined || this.form.workTypeId === 0
                  ? 0
                  : this.form.workTypeId
            }
            resourcesave(param).then((response) => {
              if (response.code == '200') {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      this.editExportVisible = true
    }
  }
}
</script>
